#!/usr/bin/env python
#coding=utf-8
'''
-查询员工信息，可以从id查询，可以从条目内容查询
-查询从三个字母开始
-匹配内容高亮
'''
import sys,re,os
from sys import argv

class bcolors:
	'''colors clss'''
	HEADER = '\033[95m'
	OK_BLUE = '\033[94m'
	OK_GREEN = '\033[92m'
	WARING_YELLOW = '\033[93m'
	FAIL = '\033[36m'
	FLASHING = '\033[35m'
	CRITICAL_RED = '\033[31m'
	END = '\033[0m'

def id_search():
	'''use id search function'''
	while True:
		id_num = raw_input("search id: ")
		#如果输入的序号在字典的key中，打印后续所有信息，否则给出提示，并继续循环
		if id_num in dict_employee:
			print bcolors.OK_GREEN + "Found 1 items: " + bcolors.END
			print "  id          ：" + "%s" % id_num + bcolors.END
			print "  name        ：" + "%s" % dict_employee[id_num][0]
			print "  phone_num   ：" + "%s" % dict_employee[id_num][1]
			print "  gender      ：" + "%s" % dict_employee[id_num][2]
			print "  department  ：" + "%s" % dict_employee[id_num][3] + '\n'
		else:
			print bcolors.CRITICAL_RED + "Sorry, id_num %s not found，please check again. " % id_num + bcolors.END

		#处理用户搜索结束提示是否继续，并忽略用户输入的退出的大小写内容
		con_search = raw_input(bcolors.WARING_YELLOW + "press any continue, or exit " + bcolors.END)
		if con_search.lower() == 'exit':
			sys.exit(bcolors.OK_BLUE + 'thanks useing, bye-bye.' + bcolors.END)

def content_search():
	'''use content search funciton'''
	while True:
		search = raw_input("search content: ")

		#对用户输入的长度做限制，只有大于3个字符给与匹配
		if len(search) <= 2:
			print bcolors.CRITICAL_RED + "input search content too short. " + bcolors.END
		else:
			#初始化匹配条目，匹配一次，累加一次
			search_count = 0
			#遍历字典，变value为字符串，进行匹配数的处理
			for id, search_content in dict_employee.items():
				match = "".join(search_content)

				#输入字符串和处理过的字符串进行正则查找，查找到匹配条目数加1
				if re.findall(search, match, re.MULTILINE):
					search_count += 1

			print bcolors.OK_GREEN + "found %d itmes" % search_count + bcolors.END

			#对匹配内容进行着色处理
			os.system('grep --color %s employee.txt' % search)

			#友情提示部分
			con_search = raw_input('\n' + bcolors.WARING_YELLOW + "press any continue, or exit " + bcolors.END)
			if con_search.lower() == 'exit':
				sys.exit(bcolors.OK_BLUE + 'thanks useing, bye-bye.' + bcolors.END)

def program_exit():
	'''exit function'''
	sys.exit(bcolors.FAIL + "Thank you for useing. Bye-Bye~\n" + bcolors.END)

def more_features():
	'''more_features function'''
	print bcolors.FAIL + "More features has developed in future. " + bcolors.END

if __name__ == '__main__':
	#接收一个参数，初始化一个空字典储存员工信息
	script, employee = argv
	dict_employee = {}

	#遍历文件，将文件的字段加入数组变成字典的key对应的values
	lines = open(employee).readlines()
	for line in lines:
		id_num, name, phone, gender, program = line.strip().split()
		dict_employee[id_num] = [name, phone, gender, program]

	print bcolors.OK_GREEN + '''
=== thanks use sample employee search program, you can type ===
++                      1、id_search                         ++
++                      2、content_search                    ++
++                      3、exit program                      ++
++                      4、more features                     ++
	''' + bcolors.END
	#用户选择
	choose = raw_input(bcolors.WARING_YELLOW + "please input your choose number: " + bcolors.END)
	if choose == '1':
		id_search()
	elif choose == '2':
		content_search()
	elif choose == '3':
		program_exit()
	elif choose == '4':
		more_features()
	else:
		print bcolors.FAIL + "Sorry, error input, please try again. " + bcolors.END



